import {GCtrl} from "../../lib/util/GCtrl";
import BaseComponent from "../../Game/Base/BaseClass/BaseComponent";


const {ccclass, property} = cc._decorator;

@ccclass
export default class DWC_ZengSong extends BaseComponent {

    @property(cc.Label)
    lbTargetZH: cc.Label = null;

    @property(cc.Label)
    lbCanUse: cc.Label = null;

    @property(cc.EditBox)
    editSend: cc.EditBox = null;

    @property(cc.EditBox)
    editYanZhenMa: cc.EditBox = null;

    danwei = 100;
    start () {

    }

    init(args){
        this.initUI();
    }

    initUI(){
        let playerInfo = GCtrl.session.getPlayer() ;// ;
        this.lbTargetZH.string = `目标账号: ${  playerInfo.parent_rid}` ; //上级代理
        this.lbCanUse.string = `可用积分: ${GCtrl.setDecimalNum((playerInfo.coin / this.danwei) + "", 0)}` //;
    }

    btnClickYanZhenMa(){
        let sendScoreStr = this.editSend.string;
        if(!this.checkCanTrunNum(sendScoreStr) ){
            //提示输入不对
            return ;
        }

        //发送验证码
        this.sendVerifyCode();
        cc.vv.audioUtil.playSound("/Hall/sound/button_onclicked");
    }

    btnClickSure(){
        //
        let playerInfo = GCtrl.session.getPlayer();
        let cmd = "Transfer";
        let msg: any = {
            toRid:playerInfo.parent_rid,                //目标id
            coin:parseInt(this.editSend.string) * this.danwei,        //赠送金额
            vcode:parseInt(this.editYanZhenMa.string),            //验证码
        }
        GCtrl.GLUtils.sendRequest(cmd, msg, (error, data) => {
            if (data.errcode)
                GCtrl.GLUtils.showTips(data.errcodedes);
            else {
                GCtrl.GLUtils.showTips("赠送成功");
                this.initUI();
            }
        });

    }

    btnClickCancel(){
        GCtrl.UIMgr.close(GCtrl.GLConfig.DWC_ZengSong_path, GCtrl.UIMgr.closeType.hide);
        cc.vv.audioUtil.playSound("/Hall/sound/button_onclicked");
    }

    checkCanTrunNum(str){
        let can = true;
        for(let i in str){
            if(str[i] >= '0' && str[i] <= "9"){

            }else {
                return false;
            }
        }
        return can;
    }

    private sendVerifyCode() {
        let playerInfo = GCtrl.session.getPlayer();
        let cmd = "GetVcode";
        let msg: any = {
            phone_number: playerInfo.phone
        }
        GCtrl.GLUtils.sendRequest(cmd, msg, (error, data) => {
            if (data.errcode)
                GCtrl.GLUtils.showTips(data.errcodedes);
            else {
                GCtrl.GLUtils.showTips("发送成功");
            }
        });
    }
}
